<?php
/**
 * 用户账号
 */
namespace app\common\model;

use think\Model;

class AccountAddress extends Model
{
    protected $name = 'account_address';

    /**
     * 获取所有账号列表
     * @param array $where 查询条件
     * @param string $filed 字段查询
     * @param string $order 排序
     * @return AccountAddress[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getListAll($where = [],$filed = '',$order = 'id desc'){
        $data = self::where($where);
        return $data->order($order)->select();
    }

    /**
     * 查询列表
     * @param int $page 页码
     * @param int $limit    页面数量
     * @param array $where 查询条件
     * @param string $order 排序
     * @return array
     * @throws \think\db\exception\DbException
     */
    public static function getList($page=1,$limit=20,$where=[],$order = 'id desc'){
        $data = self::where($where)->order($order)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();
        return $data;
    }

    /**
     * 详情
     * @param $id ID
     * @param string $field 详情
     * @return AccountAddress|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getDetails($id,$field = ''){
        $data = self::where('id',$id);
        if(!empty($field))$data = $data->field($field);
        return $data->find();
    }

    /**
     * 详情
     * @param $address 用户地址
     * @param string $field 字段关联
     * @return AccountAddress|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAddressDetails($address,$field = ''){
        $data = self::where('address',$address);
        if(!empty($field))$data = $data->field($field);
        return $data->find();
    }

    /**
     * 获取未分配的账号
     * @return mixed
     */
    public static function getRand(){
        return self::orderRaw('rand()')->where(['status'=>1])->order('id asc')->field('address')->find();
    }
}